#!/usr/bin/env python
import sys
import class_set
import helper
from sets import Set

class FeedRecommendationReducer(helper.ReducerHelper):

    def trasferReason(self, reason):
        if reason == 'like':
            return '102'
        elif reason == 'comment':
            return '101'
        elif reason == 'resource':
            return '103'
        else:
            return None

    def processor(self, key, lst):
        res = {}
        for e in lst:
            item_id, tag = self.split(e)
            recom_tag = class_set.RecomTagFeed('')
            recom_tag.init(tag)
            if item_id not in res:
                res[item_id] = []
            res[item_id].append(recom_tag)

        uid, reason = self.split(key)
        recom_result = class_set.RecomResult()
        recom_result.ppuid = uid
        recom_result.reason_id = self.trasferReason(reason)
        recom_result.reason_desc = reason
        for e in res.keys():
            recom_atom = class_set.RecomAtom()
            recom_atom.item_id = e
            for l in res[e]:
                recom_atom.involved_ids.append(l.getUid())
            recom_atom.involved_num = len(recom_atom.involved_ids)
            recom_result.resys_items.append(recom_atom)
        if len(recom_result.resys_items) != 0:
            print recom_result
            

def main():
    fr = FeedRecommendationReducer()
    fr.reducer()

if __name__ == '__main__':
    main()









